﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Newtonsoft.Json;
using DevExpress.XtraEditors;
using DexERP.Core.Utility;

namespace DexERP
{
    public partial class XtraFormCfg : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public XtraFormCfg()
        {
            InitializeComponent();
        }

        private void XtraFormCfg_Load(object sender, EventArgs e)
        {
            //barStaticItemComp.Caption = DexGlobal.User.CompanyName;
            //barStaticItemUser.Caption = DexGlobal.User.UserName;

            LoadDatas();
        }
        private void LoadDatas()
        {
            List<DTO.Connections> conns = new List<DTO.Connections>();
            List<DTO.DexConfig> datas = new List<DTO.DexConfig>();
            gridControlM.DataSource = null;
            vGridControlM.DataSource = null;

            if (DexGlobal.SysCfg != null)
            {
                conns = DexGlobal.SysCfg.Conns;
            }
            else
            {
                conns.Add(new DTO.Connections()
                {
                    caption = "罗斯库",
                    host = "192.168.0.160",
                    uid = "EpicorERP",
                    pwd = "ross2016",
                    cfgpath = @"C:\Epicor\ERP10.1Client\Client\config\Live.sysconfig"
                });
            }
            
            DTO.DexConfig cfg = new DTO.DexConfig();
            cfg.ProductName = Application.ProductName;
            cfg.Version = Application.ProductVersion;
            datas.Add(cfg);

            vGridControlM.DataSource = datas;
            gridControlM.DataSource = conns;

            string[] captionArr = ("链接库,主机,用户名,密码,端口号,库名,配置文件").Split(',');
            for (int i = 0; i < captionArr.Length; i++)
            {
                gridViewM.Columns[i].Caption = captionArr[i];
            }
        }
        private void barButtonItemSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            DTO.DexConfig cfg = new DTO.DexConfig();
            cfg.ProductName = Application.ProductName;
            cfg.Version = Application.ProductVersion;
            cfg.Conns = gridControlM.DataSource as List<DTO.Connections>;
            string jsons = JsonConvert.SerializeObject(cfg);
            FileHelper.TxtWrite(DexGlobal.ConfigPath, jsons);
            XtraMessageBox.Show("设置成功！", "提示");
        }

        private void barButtonItemAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            var datas = gridControlM.DataSource as List<DTO.Connections>;
            DTO.Connections newItem = new DTO.Connections();
            newItem.caption = "新连接";
            datas.Add(newItem);
            gridControlM.DataSource = datas;
            gridControlM.MainView.RefreshData();
        }

        private void barButtonItemRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            LoadDatas();
        }

        private void barButtonItemDel_ItemClick(object sender, ItemClickEventArgs e)
        {
            var datas = gridControlM.DataSource as List<DTO.Connections>;
            int iSelectRowCount = gridViewM.SelectedRowsCount;
            if (iSelectRowCount > 0)
            {
                gridViewM.DeleteSelectedRows();
            }
            gridControlM.DataSource = datas;
            gridControlM.MainView.RefreshData();
        }
    }
}